package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;
import org.json.JSONException;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 *
 * @Title: HST_DownloadRecord
 * @Description: 华盛证券录音转换
 * @author: chieh
 * @date 2021-10-08 12:30:00
 *
 */
public class HST_DownloadRecord implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(HST_DownloadRecord.class);

	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {

		log.debug("-------------------------start HST_DownloadRecord 开始-----------------");

		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("utf-8");
		//获取参数aes_str,需加密的字符串
		def uniqueid = request.getParameter("uniqueid");
		log.debug("uniqueid:" + uniqueid);

		//获取配置信息
		//String fileUrl="http://192.168.90.19:8080/api/pbx/recordfile?uniqueid=M-1632908491.701";
		//String fileLocal="D:/downloadtest/M-1632908491.701.wav";

		def getSelectSQL = eliteBean.chaxun_sql("select  DATE_FORMAT(now(),'%Y%m%d') as nowtime", "dataSource");
		String time = getSelectSQL.get(0).get("nowtime");//附件路径临时日期的判断

		//检查知识库路径下文件夹是否存在
		String FILEURL = getProperty.get("MY.fileLocal");//存储附件的路径 /nfs/ngsfiles/record/

		//检查每日生成的时间路径是否存在
		String FILEURLtime = FILEURL + time;

		//路径是否存在
		File filefolder = new File(FILEURLtime);
		//如果文件夹不存在则创建
		if (!filefolder.exists() && !filefolder.isDirectory()) {
			log.debug("//不存在");
			filefolder.mkdir();
		} else {
			log.debug("//目录存在");
		}

		String fileUrl = getProperty.get("MY.fileUrl")+uniqueid;//http://192.168.90.19:8080/api/pbx/recordfile?uniqueid=
		String fileLocal = FILEURLtime+"/"+uniqueid+'.wav';//

		log.debug("fileUrl:" + fileUrl+"	fileLocal:" + fileLocal);
		long startTime = System.currentTimeMillis();

		def reJson = "";
		try {

			downloadFile(fileUrl,fileLocal);
			//内网访问ngs地址 https://callcenter-in.hszq8.com/ngs
			String intraneturl=getProperty.get("RECORD.fileUrl")
			reJson=intraneturl+"/fs/get?file=record/"+time+"/"+uniqueid+'.wav';
		} catch (Exception e) {
			log.debug(e.getMessage());
			reJson="";
		}
		return reJson;
	}

	/** * 文件下载 * @param fileUrl
	 * 下载路径 * @param savePath
	 * 存放地址 示例：D:/ceshi/1.png
	 ** @throws Exception */
	public static void downloadFile(String fileUrl,String savePath) throws Exception {
		File file=new File(savePath);
		//判断文件是否存在，不存在则创建文件
		if(!file.exists()){
			file.createNewFile();
		}
		URL url = new URL(fileUrl);
		HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
		urlCon.setConnectTimeout(6000);
		urlCon.setReadTimeout(6000);
		int code = urlCon.getResponseCode();
		if (code != HttpURLConnection.HTTP_OK){
			throw new Exception("文件读取失败");
		}
		DataInputStream in1 = new DataInputStream(urlCon.getInputStream());
		DataOutputStream out = new DataOutputStream(new FileOutputStream(savePath));
		byte[] buffer = new byte[2048];
		int count = 0;
		while ((count = in1.read(buffer)) > 0) {
			out.write(buffer, 0, count);
		}
		try {
			if(out!=null) {
				out.close();
			}
			if(in1!=null) {
				in1.close();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}

	}
}